Movie = require('../models/movie')
Category = require('../models/category')
Comment = require('../models/comment')
_ = require('underscore')
fs = require('fs')
path = require('path')

# detail page
exports.detail = (req,res)->
  id = req.params.id
  Movie.update {_id:id}, {$inc: {pv:1}}, (err)->
    console.log err if err
  Movie.findById id, (err, movie)->
    Comment
      .find {movie: id}
      .populate('from', 'name')
      .populate('reply.from reply.to', 'name')
      .exec (err, comments)->
        res.render 'detail', {
          title: '详情页',
          movie: movie,
          comments: comments
        }

# admin new page
exports.new = (req, res)->
  res.render 'admin', {
    title: 'movie后台录入页',
    movie: {
      title: '',
      doctor:'',
      country:'',
      year:'',
      poster:'',
      flash:'',
      summary:'',
      language:''
    }
  }

# admin new page
exports.new = (req, res)->
  Category.find {}, (err, categories)->
    res.render 'admin', {
      title: 'movie后台录入页',
      categories: categories,
      movie: {}
    }
#admin update movie
exports.update = (req, res)->
  id = req.params.id
  if id
    Movie.findById id, (err, movie)->
      Category.find {}, (err, categories)->
        res.render 'admin', {
          title: 'movie后台更新页',
          categories: categories,
          movie: movie
        }
#上传
exports.savePoster = (req, res, next)->
  posterData = req.files.poster
  filePath = posterData.path
  originalFilename = posterData.originalFilename
  # console.log(req.files)

  if originalFilename
    fs.readFile filePath, (err, data)->
      timestamp = Date.now()
      type = posterData.type.split('/')[1]#jpg
      poster = timestamp + '.' + type
      newPath = path.join(__dirname, '../../', '/public/upload/' + poster)
      fs.writeFile newPath, data, (err)->
        req.poster = poster
        next()
  else
    next()
#admin post movie
exports.save = (req, res) ->
  id = req.body.movie._id
  movieObj = req.body.movie
  _movie
  if req.poster
    movieObj.poster = req.poster
  console.log "1:_" + id
  if id
    Movie.findById id, (err, movie) ->
      console.log err if err
      console.log JSON.stringify movie
      console.log JSON.stringify movieObj
      _movie = _.extend(movie, movieObj)
      _movie.save (err, movie) ->
        console.log err if err
        res.redirect '/movie/' + movie._id
        return
      return
  else
    _movie = new Movie movieObj

    categoryId = movieObj.category
    categoryName = movieObj.categoryName

    _movie.save (err, movie) ->
      return err if err
      if categoryId#如果存在分类
        Category.findById categoryId, (err, category)->
          category.movies.push(movie._id)
          category.save (err, category)->
            res.redirect '/movie/' + movie._id
      else if categoryName
        category = new Category
          name: categoryName,
          movies: [movie._id]
        category.save (err, category)->
          movie.category = category._id
          movie.save (err,movie)->
            res.redirect '/movie/' + movie._id

      return
    return

# list page
exports.list = (req, res)->
  Movie.fetch (err,movies)->
    console.log err if err
    res.render 'list', {
      title: 'movie后台列表页',
      movies: movies
    }

# list delete movie
exports.del = (req, res)->
  id = req.query.id
  if id
    Movie.remove {_id: id}, (err, movie)->
      console.log err if err
  else
    res.json {success: 1}
